package modele.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import modele.beans.Building;
import modele.database.IDatabase;

public class BuildingDAO {
	private IDatabase conn;
	
	public BuildingDAO(IDatabase conn) {
		this.conn = conn; 
	}
	
	public ArrayList<Building> GetBuildings() throws SQLException, ClassNotFoundException {
		conn.Connect();
		ArrayList<ArrayList<Object>> buildingList = conn.Select("SELECT NAME, ADDRESS, PHONE, BUILDING_ID FROM BUILDING");
		
		ArrayList<Building> buildings = new ArrayList<Building>();
		
		for (int i = 0; i != buildingList.size(); ++i) {
			buildings.add(new Building((String)buildingList.get(i).get(0),
					(String)buildingList.get(i).get(1),
					(String)buildingList.get(i).get(2),
					buildingList.get(i).get(3).toString()));
		}
		
		conn.Close();
		
		return buildings;
	}
	
	public void AddBuilding(Building building) throws SQLException, ClassNotFoundException {
		conn.Connect();
		conn.Insert("INSERT INTO BUILDING(NAME, ADDRESS, PHONE, BUILDING_ID) "
					+ " SELECT ?, ?, ?, building_seq.nextval FROM DUAL", 
					building.getName(), building.getAddress(), building.getPhoneNumber());
		conn.Close();
	}
	
	public void UpdateBuilding(Building building) throws SQLException, ClassNotFoundException {
		conn.Connect();
		conn.Update("UPDATE BUILDING " +
					" SET NAME = ?, " +
					" ADDRESS = ?, " +
					" PHONE = ?" +
					" WHERE BUILDING_ID = ?",
					building.getName(), building.getAddress(), building.getPhoneNumber(), building.getId());
		conn.Close();
	}
	
	public void RemoveBuilding(Building building) throws SQLException, ClassNotFoundException {
		conn.Connect();
		conn.Update("DELETE FROM BUILDING " +
					" WHERE BUILDING_ID = ? ", building.getId());
		conn.Close();
	}
}
